<?php

namespace App\Service;

use Illuminate\Support\Facades\Log;

class CommonService extends BaseService {

    const CLOCK_DISTANCE_ERROR = 500; //打卡误差范围

    public function __construct(

    ){

    }

    public function uploadImg() {

    }

    public static function reqLog($req){
        Log::info('req',['req'=>json_encode($req->all()),'url'=>$req->url(),'REQUESTID'=>REQUESTID,'time'=>date("Y-m-d H:i:s"),'headerInfo'=>$req->header(),'apiSession'=>session('userInfo') ?? '','adminSession'=>session('adminInfo') ?? '']);
    }
    public static function respLog($resp){
        Log::info('resp',['resp'=>json_encode($resp),'REQUESTID'=>REQUESTID,'time'=>date("Y-m-d H:i:s"),'apiSession'=>session('userInfo') ?? '','adminSession'=>session('adminInfo') ?? '']);
    }

    public static function getExceptionInfo(int $code) {
        $data = [
            1005 => '课程时间冲突',
            1006 => '无法为当前校区排课',
            1007 => '课程级别不存在',
            1008 => '账户不存在',
            1009 => '密码错误',
            1010 => '打卡信息错误，请联系管理员',
            1011 => '无法签退',
        ];
        return $data[$code] ?? '系统异常，未知错误';
    }

    /**
     * 获取两组经纬度距离
     * @param $lng1
     * @param $lat1
     * @param $lng2
     * @param $lat2
     * @return float|int
     */
    public static function getDistance($lng1, $lat1, $lng2, $lat2) {
        // 将角度转为狐度
        $radLat1 = deg2rad($lat1); //deg2rad()函数将角度转换为弧度
        $radLat2 = deg2rad($lat2);
        $radLng1 = deg2rad($lng1);
        $radLng2 = deg2rad($lng2);
        $a = $radLat1 - $radLat2;
        $b = $radLng1 - $radLng2;
        $s = 2 * asin(sqrt(pow(sin($a / 2), 2) + cos($radLat1) * cos($radLat2) * pow(sin($b / 2), 2))) * 6378.137 * 1000;
        return $s;
    }
}
